﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.Areas.BizProj.Models;
using ProjBackEnd.Lib;
using ProjBackEnd.DAL;

namespace ProjBackEnd.Areas.BizProj.Controllers
{
    /// <summary>
    /// 业务合同操作
    /// </summary>
    public class BizContractOptController : ApiController
    {
        /// <summary>
        /// 获取业务合同列表
        /// eg.：0000000001|TST|WEB|20|1|0|0|0|9|2018-11-01|
        /// </summary>
        /// <param name="key">memberid|comcd（空值=所有）|登录平台（WEB/PC/APP）|每页记录数|当前页数（从1开始）|项目id（0=所有）|合同id（0=所有）|客户id（0=所有）|Pid（空值=所有）|合同签订开始时间（空值=所有）|合同签订结束时间（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/bizcontractopt")]
        public RespBizContract GetBizContract(string key)
        {
            RespBizContract resp = new RespBizContract();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 11)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strPid = strsKeyOriginal[8];

            int iProjid,iCtid, iCusid, iPageSize, iCurrPage;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) 
                || !int.TryParse(strsKeyOriginal[4], out iCurrPage)
                || !int.TryParse(strsKeyOriginal[5], out iProjid)
                || !int.TryParse(strsKeyOriginal[6], out iCtid)
                || !int.TryParse(strsKeyOriginal[7], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            DateTime? dtSdt = null;
            DateTime? dtEdt = null;
            try
            {

                if (!string.IsNullOrEmpty(strsKeyOriginal[9])) dtSdt = DateTime.Parse(strsKeyOriginal[9]);
                if (!string.IsNullOrEmpty(strsKeyOriginal[10])) dtEdt = DateTime.Parse(strsKeyOriginal[10]).Date.AddDays(1);
            }
            catch
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.GetBizContractPaging(strMemberid, strComcd, iPageSize, iCurrPage, iProjid, iCtid, iCusid, strPid, dtSdt, dtEdt);
            return resp;
        }

        /// <summary>
        /// 更新业务合同基本信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/bizcontractopt")]
        public RespProjNullResultInfo UpdateBizContractBaseInfo(ReqBizContract _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateBizContractBaseInfo(_reqInfo.memberid, _reqInfo.contbaseinfo);
            return resp;
        }

        /// <summary>
        /// 更新业务合同敏感信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/bizcontractoptsens")]
        public RespProjNullResultInfo UpdateContProcess(ReqBizContractSens _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateBizContractProc(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.contcoreinfo);
            return resp;
        }

        /// <summary>
        /// 获取业务合同子项列表（所有id不能同时为0）
        /// eg.：0000000001|TST|WEB|0|0|0
        /// </summary>
        /// <param name="key">memberid|comcd（空值=所有）|登录平台（WEB/PC/APP）|项目id（0=所有）|合同id（0=所有）|合同分项id（0=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/bizcontractsubopt")]
        public RespBizContractSub GetBizContractSub(string key)
        {
            RespBizContractSub resp = new RespBizContractSub();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 6)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            int iProjid, iCtid, iCtSubid;
            if (!int.TryParse(strsKeyOriginal[3], out iProjid)
                || !int.TryParse(strsKeyOriginal[4], out iCtid)
                || !int.TryParse(strsKeyOriginal[5], out iCtSubid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            //所有id不能同时为0
            if (iProjid == 0 && iCtid == 0 && iCtSubid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            resp = projLogic.GetBizContractSub(iProjid, iCtid, iCtSubid);
            return resp;
        }

        /// <summary>
        /// 更新业务合同子项信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/bizcontractsubopt")]
        public RespProjNullResultInfo UpdateBizContractSubBaseInfo(ReqBizContractSub _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateBizContractSub(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.ctsubcoreinfo);
            return resp;
        }

        /// <summary>
        /// 更新业务合同子项敏感信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/bizcontractsuboptsens")]
        public RespProjNullResultInfo UpdateContSubProcess(ReqBizContractSub _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateBizContractSubProc(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.ctsubcoreinfo);
            return resp;
        }


    }
}
